/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * CommissionRulesServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.CommissionRuleParentMapper;
import com.yixun.qfbao.mapper.CommissionRulesMapper;
import com.yixun.qfbao.model.CommissionRuleParent;
import com.yixun.qfbao.model.CommissionRules;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.dto.CommissionRuleParentDto;
import com.yixun.qfbao.model.dto.CommissionRulesDto;
import com.yixun.qfbao.model.dto.RuleDto;
import com.yixun.qfbao.model.enums.CommisionEnum;
import com.yixun.qfbao.model.enums.CommisionStateEnum;
import com.yixun.qfbao.model.enums.CommissionStatusEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.CommissionRulesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * [文件名称]<br>
 * CommissionRulesServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 佣金规则表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-10-18 18:27:59 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("commissionRulesService")
public class CommissionRulesServiceImpl implements CommissionRulesService {
	
	@Autowired
	private CommissionRulesMapper mapper;
	@Autowired
	private CommissionRuleParentMapper commissionRuleParentMapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Override
	@Transactional(readOnly = true)
	public Page<CommissionRules> getPageModel(CommissionRulesDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<CommissionRules> rows = mapper.list(entity,begin,end);
	    Page<CommissionRules> msr = new Page<CommissionRules>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	@Override
	public List<CommissionRules> selectListByDto(CommissionRulesDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	@Override
	public CommissionRules selectByDto(CommissionRulesDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param
	 * @return 分页记录
	 */
	@Override
	public CommissionRules selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	@Override
	public CommissionRulesDto create(CommissionRulesDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	@Override
	public int update(CommissionRulesDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	@Override
	public int destroy(CommissionRulesDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	@Override
	public int saveOrUpdate(CommissionRulesDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public CommissionRules selectBy(CommissionRules entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	@Override
	public CommissionRulesDto getSetCommissionDetail(Long id) {

		return mapper.getSetCommissionDetail(id);
	}
	/**
	 * 新增佣金规则
	 * @author sangtao
	 * @param dto
	 * @return
	 */
	@Override
	@Transactional
	public int addCommissionRule(CommissionRulesDto dto){
		int result = 0;
		//参数验证
		//1.添加父表数据
		CommissionRuleParent parent = new CommissionRuleParent();
		parent.setPremisesId(dto.getPremisesId());
		parent.setDelFlag(DelFlagEnum.UNDEL.getNo());
		parent.setCreateTime(Calendar.getInstance().getTime());
		parent.setState(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_1.getCode());
		parent.setCreateUserId(dto.getCreateUserId());
		//首次添加 计算开始时间，结束时间
//		Map<String,Date> beginAndEndTime = setBeginAndEndTime(dto);
		Map<String,Date> beginAndEndTime = calculateCycle(dto.getCommissionType(), dto.getSettlementCycle(), dto.getEffectTime(),dto.getLostTime());;
		parent.setBeginTime(beginAndEndTime.get("beginTime"));
		parent.setEndTime(beginAndEndTime.get("endTime"));


		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		dto.setState(CommisionStateEnum.COMMISION_STATE_2.getNo());
		//2.添加子表规则
		//固定
		if(CommisionEnum.COMMISSION_TYPE_FIXED_AMOUNT.getCode().equals(dto.getCommissionType())
			|| CommisionEnum.COMMISSION_TYPE_FIXED_RATE.getCode().equals(dto.getCommissionType())){
			for(Company c : dto.getCooperations()){
				parent.setCompanyId(c.getId());
				//校验ruleParent是否已存在，如果已存在则返回ID，不存在则新增
				CommissionRuleParentDto crp = new CommissionRuleParentDto();
				crp.setPremisesId(dto.getPremisesId());
				crp.setCompanyId(c.getId());
				List<CommissionRuleParent> parentList = commissionRuleParentMapper.selectListByDto(crp);
				if (null != parentList && 0 < parentList.size()) {
					parent.setId(parentList.get(0).getId());
					parent.setUpdateTime(Calendar.getInstance().getTime());
					commissionRuleParentMapper.edit(parent);
				}else {
					commissionRuleParentMapper.add(parent);
				}
				if(parent.getId() == null){
					throw new RuntimeException("添加规则失败");
				}
				dto.setGroupId(UUID.randomUUID().toString());
				dto.setRelationId(parent.getId() );
				dto.setSort("1");
				result += mapper.add(dto);
			}

		}
		//按阶梯需生成多条规则
		if(CommisionEnum.COMMISSION_TYPE_LADDER_AMOUNT.getCode().equals(dto.getCommissionType())
				||CommisionEnum.COMMISSION_TYPE_LADDER_COUNT.getCode().equals(dto.getCommissionType())){
			List<RuleDto> ruleList = dto.getRules();
			// 添加父表
			for(Company c : dto.getCooperations()){
				parent.setCompanyId(c.getId());
				CommissionRuleParentDto crp = new CommissionRuleParentDto();
				crp.setPremisesId(dto.getPremisesId());
				crp.setCompanyId(c.getId());
				List<CommissionRuleParent> parentList = commissionRuleParentMapper.selectListByDto(crp);
				if (null != parentList && 0 < parentList.size()) {
					parent.setId(parentList.get(0).getId());
					parent.setUpdateTime(Calendar.getInstance().getTime());
					commissionRuleParentMapper.edit(parent);
				}else {
					commissionRuleParentMapper.add(parent);
				}
				if(parent.getId()  == null){
					throw new RuntimeException("添加规则失败");
				}
				dto.setGroupId(UUID.randomUUID().toString());
				//2.添加子表规则
				for(RuleDto r :ruleList){
					dto.setRelationId(parent.getId() );
					dto.setSort(r.getOrder());
					String start =  r.getStart() == null?"":r.getStart();
					String end =  r.getEnd() == null ? "" : r.getEnd();
					dto.setRule(start + "," + end );
					dto.setCommissionRate(r.getRule());
					result +=mapper.add(dto);
				}
			}

		}
		return result;
	}

	/**
	 * 编辑佣金规则
	 * @author sangtao
	 * @param dto
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int editCommissionRule(CommissionRulesDto dto){
		int result = 0;
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd");
		//设置有效时间与有效状态
		if(CommisionStateEnum.COMMISION_STATE_2.getNo().equals(dto.getState())){
			dto.setLostTime(null);
		}else if(CommisionStateEnum.COMMISION_STATE_1.getNo().equals(dto.getState())){
			//如果是当天的取当前时间
			if(fmt.format(dto.getLostTime()).equals(fmt.format(Calendar.getInstance().getTime()))){
				dto.setLostTime(Calendar.getInstance().getTime());
			}else{
				// 大于当天的则设置为有效，直到失效日期
				dto.setState(CommisionStateEnum.COMMISION_STATE_2.getNo());
			}
		}

		//1 验证规则修改
		boolean change = isChange(dto);
		//设置开始时间，结束时间

		//更新开始  结束时间

		//Map<String, Date> beginAndEndTime = setBeginAndEndTime(dto);
		Map<String,Date> beginAndEndTime = calculateCycle(dto.getCommissionType(), dto.getSettlementCycle(), dto.getEffectTime(),dto.getLostTime());;
		CommissionRuleParent parent = new CommissionRuleParent();
		parent.setId(dto.getRelationId());
		parent.setUpdateTime(new Date());
		parent.setBeginTime(beginAndEndTime.get("beginTime"));
		parent.setEndTime(beginAndEndTime.get("endTime"));
		commissionRuleParentMapper.edit(parent);

		if(change){
			//原规则置为无效
			CommissionRulesDto editsDto = new CommissionRulesDto();
			editsDto.setState(CommisionStateEnum.COMMISION_STATE_1.getNo());
			editsDto.setLostTime(new Date());
			editsDto.setGroupId(dto.getGroupId());
			editsDto.setUpdateTime(new Date());
			editsDto.setUpdateUserId(dto.getUpdateUserId());
			mapper.editByGroupId(editsDto);
		}

		//获取当前规则下出了自身为有效外的所有有效子规则
		List<Long> idsList = new LinkedList<Long>();
		if (CommisionStateEnum.COMMISION_STATE_2.getNo().equals(dto.getState())) {
			CommissionRulesDto crDto = new CommissionRulesDto();
			crDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			crDto.setRelationId(dto.getRelationId());
			crDto.setState(CommisionStateEnum.COMMISION_STATE_2.getNo());
			List<CommissionRules> rulesList = mapper.selectListByDto(crDto);
			if(null != rulesList && rulesList.size() > 0) {
				for (CommissionRules rules : rulesList) {
					idsList.add(rules.getId());
				}
			}
		}
		//2.添加子表规则
		//固定
		if(CommisionEnum.COMMISSION_TYPE_FIXED_AMOUNT.getCode().equals(dto.getCommissionType())
				|| CommisionEnum.COMMISSION_TYPE_FIXED_RATE.getCode().equals(dto.getCommissionType())){
			dto.setSort("1");
			if(change) {
				//新增一条数据
				dto.setGroupId(UUID.randomUUID().toString());
				result = mapper.add(dto);
			}else{
				if (idsList != null && idsList.size() > 0 && CommisionStateEnum.COMMISION_STATE_2.getNo().equals(dto.getState())) {
					for (Long id : idsList) {
						myEdit(id);
					}
				}

				dto.setId(dto.getSubRuleId());
				result = mapper.editForRule(dto);
			}
		}
		//按阶梯需生成多条规则
		if(CommisionEnum.COMMISSION_TYPE_LADDER_AMOUNT.getCode().equals(dto.getCommissionType())
				||CommisionEnum.COMMISSION_TYPE_LADDER_COUNT.getCode().equals(dto.getCommissionType())){
			List<RuleDto> ruleList = dto.getRules();
			String uuid = UUID.randomUUID().toString();

			if (!change && idsList!= null && idsList.size() > 0 && CommisionStateEnum.COMMISION_STATE_2.getNo().equals(dto.getState())) {
				for (Long id : idsList) {
					myEdit(id);
				}
			}

			for(RuleDto r :ruleList){
				dto.setSort(r.getOrder());
				String start =  r.getStart() == null?"":r.getStart();
				String end =  r.getEnd() == null ? "" : r.getEnd();
				dto.setRule(start + "," + end);
				dto.setCommissionRate(r.getRule());
				if(change){
					//新增一条数据
					dto.setGroupId(uuid);
					result += mapper.add(dto);
				}else{
					dto.setId(r.getId());
					result += mapper.editForRule(dto);
				}

			}


		}
		return result;
	}

	public int myEdit(Long id) {
		CommissionRulesDto editsDto = new CommissionRulesDto();
		editsDto.setState(CommisionStateEnum.COMMISION_STATE_1.getNo());
		editsDto.setLostTime(new Date());
		editsDto.setUpdateTime(new Date());
		editsDto.setId(id);
		return mapper.edit(editsDto);
	}

	@Override
	public Map<String,Object> getCommissionRulesForEdit(CommissionRulesDto dto){
		Map<String,Object> map = new HashMap<String,Object>();
		List<RuleDto> listRule = new ArrayList<RuleDto>();
		List<CommissionRules> list = mapper.selectListByDto(dto);
		for(CommissionRules cr : list){
			RuleDto rd = new RuleDto();
			rd.setId(cr.getId());
			rd.setOrder(cr.getSort());
			rd.setRule(cr.getCommissionRate());
			if (StringUtils.isNotBlank(cr.getRule())) {
				String tmp = cr.getRule();
				String[] rules = tmp.split(",");
				if("1".equals(cr.getSort())){
					rd.setEnd(rules[rules.length-1]);
				}else if("2".equals(cr.getSort())){
					rd.setStart(rules[0]);
					rd.setEnd(rules[rules.length-1]);
				}else if("3".equals(cr.getSort())){
					rd.setStart(rules[0]);
				}
			}
			listRule.add(rd);
		}
		if(list != null && list.size() > 0){
			map.put("commission",list.get(0));
		}else{
			map.put("commission",null);
		}
		map.put("rules",listRule);

		return map;
	}

	/**
	 *@Author sangtao
	 *@Date 2017/10/28
	 *@param
	 **/
	@Override
	public int setCommissionRuleTime(){
		//1: 分页查询所有parentRule
		//2: 遍历规则详情查找周期
		//3: 更新parentRule开始时间，结束时间
		int result = 0;
		int perNo = 300;
		CommissionRuleParentDto dtoParent = new CommissionRuleParentDto();
		dtoParent.setDelFlag(DelFlagEnum.UNDEL.getNo());
		Date now = Calendar.getInstance().getTime();
		int count = commissionRuleParentMapper.count(dtoParent);

//		while (true) {
		for(int i = 0;i < (count+perNo-1)/300;i++){
			List<CommissionRuleParent> list = commissionRuleParentMapper.list(dtoParent, perNo*i, perNo);
			if (list == null || list.size() == 0) {
				break;
			}
			for (CommissionRuleParent cp : list) {
				//在有效时间内的最新的一条子规则，查询出其结算周期
				CommissionRulesDto dtoRule = new CommissionRulesDto();
				dtoRule.setRelationId(cp.getId());
				dtoRule.setEffectTime(Calendar.getInstance().getTime());

				List<CommissionRulesDto> rules = mapper.getCommissionForEffective(dtoRule);

				CommissionRuleParent ruleParent = null;
				String type = null;
				String cycle = null;

				//只匹配最新的规则
				if (rules != null && rules.size() > 0) {
					CommissionRulesDto cr = rules.get(0);
					type = cr.getCommissionType();
					if (cr.getEffectTime() != null && cr.getLostTime() != null) {
						if (now.after(cr.getEffectTime()) && now.before(cr.getLostTime())) {
							cycle = cr.getSettlementCycle();
						}
					}else if (cr.getEffectTime() != null && cr.getLostTime() == null) {
						if (now.after(cr.getEffectTime())) {
							cycle = cr.getSettlementCycle();
						}
					}
					//计算周期
					if (cycle != null && cr != null) {
						Map<String, Date> mapCycle = calculateCycle(type, cycle, cr.getEffectTime(),cr.getLostTime());
						if (mapCycle.containsKey("endTime")) {
							CommissionRuleParentDto dto = new CommissionRuleParentDto();
							dto.setId(cp.getId());
							dto.setBeginTime(mapCycle.get("beginTime"));
							dto.setEndTime(mapCycle.get("endTime"));
							dto.setUpdateTime(now);
							dto.setUpdateUserId(0L);
							result += commissionRuleParentMapper.updateCommissionRuleParentCycle(dto);
						}
					} else {
//						CommissionRuleParentDto dto = new CommissionRuleParentDto();
//						dto.setId(cp.getId());
//						dto.setUpdateTime(now);
//						dto.setUpdateUserId(0L);
//						result += commissionRuleParentMapper.updateCommissionRuleParentCycle(dto);
					}

				}
			}
		}
		return result;
	}

	/**
	 * 根据失效时间设置佣金状态
	 *@Author sangtao
	 *@Date 2017/10/31
	 *@param
	**/
	@Override
	public int setCommissionRuleState(){

		int result = 0;
		//设置当前时间
		Calendar now = Calendar.getInstance();
		now.set(Calendar.HOUR,0);
		now.set(Calendar.MINUTE,0);
		now.set(Calendar.SECOND,0);

		int perNo = 300;
		//查询数据
		CommissionRulesDto dto = new CommissionRulesDto();
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		dto.setState("1");
		dto.setOrders(" id");

		int count = mapper.count(dto);
		if(count > 0){
			//分页查询数据
			for(int i = 0;i < (count+perNo-1)/300;i++){
				List<CommissionRules> list = mapper.list(dto,perNo*i,perNo);
				for(CommissionRules cr : list){
					if(cr.getLostTime() != null && cr.getLostTime().before(now.getTime())){
						//已过失效日期
						cr.setState("0");
						result += mapper.edit(cr);
					}
				}
			}
		}

		return result;
	}


	/**
	 *	比较编辑的规则
	 *  修改失效状态，失效时间  做编辑
	 *  修改规则，修改生效周期都算作新增一条规则
	 */
	private boolean isChange(CommissionRulesDto dto){
		//获取原数据
		CommissionRulesDto condition  = new CommissionRulesDto();
		condition.setGroupId(dto.getGroupId());
		condition.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<CommissionRules> list = mapper.selectListByDto(condition);
		if(list == null || list.size() == 0){
			throw new RuntimeException("不存在此规则");
		}
		//判断修改的参数
		//1 规则类型改变
		if(!list.get(0).getCommissionType().equals(dto.getCommissionType())){
			return true;
		}
		//2 周期是否改变
		if(!list.get(0).getSettlementCycle().equals(dto.getSettlementCycle())){
			return true;
		}

		//固定
		if(CommisionEnum.COMMISSION_TYPE_FIXED_AMOUNT.getCode().equals(dto.getCommissionType())){
			if(!list.get(0).getCommissionAmmount().equals(dto.getCommissionAmmount())){
				return true;
			}
		}else if(CommisionEnum.COMMISSION_TYPE_FIXED_RATE.getCode().equals(dto.getCommissionType())){
			if(!list.get(0).getCommissionRate().equals(dto.getCommissionRate())){
				return true;
			}
		}
		//阶梯
		else if(CommisionEnum.COMMISSION_TYPE_LADDER_AMOUNT.getCode().equals(dto.getCommissionType())
				||CommisionEnum.COMMISSION_TYPE_LADDER_COUNT.getCode().equals(dto.getCommissionType())){
			Map<String,CommissionRules> map = new HashMap<String, CommissionRules>();
			if(list.size() != dto.getRules().size()){
				return true;
			}
			for(CommissionRules c : list){
				map.put(c.getSort(),c);
			}
			for(RuleDto r : dto.getRules()){
				String rule = r.getStart()+","+r.getEnd();
				//区间
				if(!rule.equals(map.get(r.getOrder()).getRule()) ){
					return  true;
				}
				//佣金比例改变
				if(!map.get(r.getOrder()).getCommissionRate().equals(r.getRule())){
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * 根据当前时间  计算实际的开始结束时间
	 *@Author sangtao
	 *@Date 2017/11/3
	 *@param type 类型
	 *@param cycle 周期
	 *
	**/
	private Map<String,Date> calculateCycle(String type,String cycle,Date effective,Date lostTime){
		if(effective == null){
			throw  new RuntimeException("生效时间在计算周期时为空");
		}
		Map<String,Date> map = new HashMap<String, Date>();
		Date begin = null;
		Date end = null;

		Integer days = Integer.parseInt(cycle);

		Calendar now = initDate(new Date());
		Calendar eff =initDate(effective);

		if(effective.after(now.getTime())){
			return firstCycle(type,cycle,effective,lostTime);
		}

		if(CommisionEnum.COMMISSION_TYPE_FIXED_AMOUNT.getCode().equals(type)||
				CommisionEnum.COMMISSION_TYPE_FIXED_RATE.getCode().equals(type)){
//			Integer addDay = ((now.get(Calendar.DAY_OF_YEAR) - eff.get(Calendar.DAY_OF_YEAR))/days )*days;
			Integer addDay = ((getIntervalDays(effective))/days )*days;
			eff.add(Calendar.DATE,addDay);
			begin = eff.getTime();
			eff.add(Calendar.DATE,days);
			end = eff.getTime();
			map.put("beginTime",begin);
			map.put("endTime",end);
		}else if(CommisionEnum.COMMISSION_TYPE_LADDER_COUNT.getCode().equals(type)||
				CommisionEnum.COMMISSION_TYPE_LADDER_AMOUNT.getCode().equals(type)){
			int monthNow = now.get(Calendar.MONTH);
			int dayNow = now.get(Calendar.DAY_OF_MONTH);
			now.set(Calendar.DAY_OF_MONTH,10);
			//当月
//			boolean thisMonth = (yearEff-yearNow) == 0 && (monthEff-monthNow) == 0;
			if("30".equals(cycle)){
				if(dayNow > 9){
					begin = now.getTime();
					now.add(Calendar.MONTH,1);
					end = now.getTime();
				}else{
					end = now.getTime();
					now.add(Calendar.MONTH,-1);
					begin = now.getTime();
				}
			}else if("90".equals(cycle)){
				int mod = monthNow%3;
				if(mod == 0 && dayNow > 9){
					begin = now.getTime();
					now.add(Calendar.MONTH,3);
					end = now.getTime();
				}else if(mod == 0 && dayNow < 10){
					end = now.getTime();
					now.add(Calendar.MONTH,-3);
					begin = now.getTime();
				}else if(mod != 0){
					now.add(Calendar.MONTH,3-mod);
					end = now.getTime();
					now.add(Calendar.MONTH,-3);
					begin = now.getTime();
				}
			}
			//生效时间 在计算周期内，开始时间为生效时间，失效时间在计算周期内，结束时间为是失效时间
			if(begin != null && end !=null){
				if(effective != null && effective.after(begin) && effective.before(end)){
					begin = effective;
				}
				if(lostTime != null && lostTime.after(begin) && lostTime.before(end)){
					end = lostTime;
				}
			}
			map.put("beginTime",begin);
			map.put("endTime",end);

		}
		return map;
	}

	/**
	 * 有效时间大于当前时间，开始时间以有效时间为准
	 *@Author sangtao
	 *@Date 2017/11/7
	 * @param type
	 * @param cycle
	 * @param effective
	 * @param lostTime
	 * @return
	 */
	private Map<String,Date> firstCycle(String type,String cycle,Date effective,Date lostTime){
		Map<String,Date> map = new HashMap<String, Date>();
		map.put("beginTime",effective);

		Calendar eff = initDate(effective);

		int day = eff.get(Calendar.DAY_OF_MONTH);
		int month = eff.get(Calendar.MONTH);

		if(CommisionEnum.COMMISSION_TYPE_FIXED_AMOUNT.getCode().equals(type)|| CommisionEnum.COMMISSION_TYPE_FIXED_RATE.getCode().equals(type)){
			eff.add(Calendar.DATE,Integer.parseInt(cycle));
		}else if(CommisionEnum.COMMISSION_TYPE_LADDER_COUNT.getCode().equals(type)|| CommisionEnum.COMMISSION_TYPE_LADDER_AMOUNT.getCode().equals(type)){
			eff.set(Calendar.DAY_OF_MONTH,10);
			// 每月10号结算
			if("30".equals(cycle)){
				if(day > 9){
					eff.add(Calendar.MONTH,1);
				}
			//每季度首月10号结算
			}else if("90".equals(cycle)){
				int remain = month % 3;
				if(remain == 0){
					if(day > 9){
						eff.add(Calendar.MONTH,3);
					}
				}else if(remain != 1){
					eff.add(Calendar.MONTH,3-remain);
				}
			}
		}
		if(lostTime != null && lostTime.before(eff.getTime())){
			map.put("endTime",lostTime);
		}else{
			map.put("endTime",eff.getTime());
		}
		return map;
	}


	private Calendar initDate(Date date){
		Calendar c = Calendar.getInstance();
		if(date != null){
			c.setTime(date);
		}
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c;
	}
	/**
	 * 计算时间区间（解决跨年问题）
	 * @param eff 生效时间
	 * @return
	 */
	private Integer getIntervalDays(Date eff){
		Calendar effective = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		effective.setTime(eff);
		now.setTime(new Date());
		int days = 0;
		while(effective.before(now)){
			days++;
			effective.add(Calendar.DAY_OF_YEAR, 1);
		}
		return days;
	}
}

